﻿Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports BusinessRule.IBusiness
Imports DatabaseAccess.IDAL
Imports System.Data
Imports BusinessRule.Common
Imports DatabaseAccess.DALFactory
Imports External.APICalling
Imports BusinessRule.DTO
Imports System.Configuration

Public Class uMES_JXUserattributeBusiness
    Private dal As IOracleDAL = uMESDalContext.instance.GetDalObject()

#Region "批次信息结转"
    Public Function SysUserattribute(ByVal serverInfo As ServerHostInfo)
        Dim result As New uMESProcessResult()
        Dim CommonObject As uMESCommonBusiness = New uMESCommonBusiness()

        '查找符合条件的批次

        Dim strSql As String = String.Format("SELECT DISTINCT c.containerid " & vbCrLf & _
                                        "FROM Datapointhistory t  " & vbCrLf & _
                                        "LEFT JOIN Datapointhistorydetail d ON d.datapointhistoryid=t.datapointhistoryid  " & vbCrLf & _
                                        "LEFT JOIN (SELECT x.containerid,x.historymainlineid,x.txndate,x.reversalstatus  " & vbCrLf & _
                                                    "FROM historymainline x JOIN (SELECT containerid,MAX(historymainlineid) AS historymainlineid  " & vbCrLf & _
                                                                                  "FROM historymainline  " & vbCrLf & _
                                                                                  "WHERE reversalstatus = 1  " & vbCrLf & _
                                                                                  "AND historymainlineid IN (SELECT DISTINCT p.historymainlineid  " & vbCrLf & _
                                                                                                            "FROM Datapointhistory p JOIN Datapointhistorydetail q ON  " & vbCrLf & _
                                                                                                            " p.Datapointhistoryid = q.Datapointhistoryid)  " & vbCrLf & _
                                                                                  "GROUP BY containerid) y ON x.historymainlineid = y.historymainlineid) h ON h.historymainlineid=t.historymainlineid  " & vbCrLf & _
                                        "LEFT JOIN container c ON c.containerid=h.containerid  " & vbCrLf & _
                                        "WHERE(c.Status = 1 And c.containerid <> c.containername And c.Parentcontainerid Is NULL And d.dataname Is Not NULL) ")

        Dim DT As DataTable = dal.StandardGetDataTable(strSql)

        For Each row As DataRow In DT.Rows
            '按批次号查找批次属性
            Dim strSql2 As String = String.Format("SELECT d.dataname,d.datavalue,c.containername,t.historyid,h.txndate  " & vbCrLf & _
                                                    "FROM Datapointhistory t  " & vbCrLf & _
                                                    "LEFT JOIN Datapointhistorydetail d ON d.datapointhistoryid=t.datapointhistoryid  " & vbCrLf & _
                                                    "LEFT JOIN (SELECT x.containerid,x.historymainlineid,x.txndate,x.reversalstatus  " & vbCrLf & _
                                                                "FROM historymainline x JOIN (SELECT containerid,MAX(historymainlineid) AS historymainlineid  " & vbCrLf & _
                                                                                              "FROM historymainline  " & vbCrLf & _
                                                                                              "WHERE reversalstatus = 1  " & vbCrLf & _
                                  "AND historymainlineid IN (SELECT DISTINCT p.historymainlineid  " & vbCrLf & _
                                                             "FROM Datapointhistory p JOIN Datapointhistorydetail q ON  " & vbCrLf & _
                                                            " p.Datapointhistoryid = q.Datapointhistoryid)  " & vbCrLf & _
                                                                                              "GROUP BY containerid) y ON x.historymainlineid = y.historymainlineid) h ON h.historymainlineid=t.historymainlineid  " & vbCrLf & _
                                                    "LEFT JOIN container c ON c.containerid=h.containerid  " & vbCrLf & _
                                                    "WHERE c.Status = 1 And c.containerid <> c.containername And c.Parentcontainerid Is NULL And d.dataname Is Not NULL And c.containerid = '{0}'",
                                                    row("containerid").ToString())

            Dim DT2 As DataTable = dal.StandardGetDataTable(strSql2)
            Dim attList As List(Of ContainerStartInfo) = New List(Of ContainerStartInfo)

            For Each row2 As DataRow In DT2.Rows
                Dim tmpEnity As ContainerStartInfo = New ContainerStartInfo()
                tmpEnity.AttributeName = row2("dataname").ToString().Trim()
                tmpEnity.attributeValue = row2("datavalue").ToString().Trim()
                tmpEnity.ContainerName = row2("containername").ToString().Trim()
                tmpEnity.Level = "Lot"
                tmpEnity.ServerHostInfo = serverInfo
                result = CommonObject.SetUserAttribute(tmpEnity)
            Next
        Next
        Return result
    End Function
#End Region

#Region "注释掉"
    ''判断批次属性是否存在
    'For Each row2 As DataRow In DT2.Rows


    '    Dim strSql3 As String = String.Format("SELECT c.containerid,c.containername,t.userattributeid,t.attributevalue,t.userattributename FROM userattribute  t " & vbCrLf & _
    '          "LEFT JOIN CONTAINER c ON c.containerid=t.parentid " & vbCrLf & _
    '          "WHERE  c.containerid ='{0}' And Userattributename = '{1}'",
    '          row("containerid").ToString().Trim(), row2("dataname").ToString().Trim())

    '    Dim DT3 As DataTable = dal.StandardGetDataTable(strSql3)
    '    '批次属性存在则更新
    '    If DT3.Rows.Count > 0 Then
    '        strSql3 = String.Format("UPDATE userattribute t SET t.attributevalue = '{0}' WHERE t.userattributeid='{1}'", row2("datavalue").ToString().Trim(), DT3.Rows(0)("Userattributeid").ToString())
    '        dal.ExecuteSql(strSql3)
    '        result.Result = True

    '    Else  '批次属性不存在直接添加到list中
    '        Dim tmpEnity As ContainerStartInfo = New ContainerStartInfo()
    '        tmpEnity.AttributeName = row2("dataname").ToString().Trim()
    '        tmpEnity.attributeValue = row2("datavalue").ToString().Trim()
    '        tmpEnity.ContainerName = row2("containername").ToString().Trim()
    '        tmpEnity.Level = "Lot"
    '        tmpEnity.ServerHostInfo = serverInfo
    '        attList.Add(tmpEnity)
    '    End If

    'Next

    ''查找批次已经存在的属性
    'Dim strSql4 As String = String.Format("SELECT c.containerid,c.containername,t.userattributeid,t.attributevalue,t.userattributename FROM userattribute  t " & vbCrLf & _
    '                                      "LEFT JOIN CONTAINER c ON c.containerid=t.parentid " & vbCrLf & _
    '                                      "WHERE  c.containerid ='{0}'",
    '                                      row("containerid").ToString().Trim())

    'Dim DT4 As DataTable = dal.StandardGetDataTable(strSql4)

    'For Each row4 As DataRow In DT4.Rows
    '    Dim tmpEnity1 As ContainerStartInfo = New ContainerStartInfo()
    '    tmpEnity1.AttributeName = DT4.Rows(0)("userattributename").ToString().Trim()
    '    tmpEnity1.attributeValue = DT4.Rows(0)("attributevalue").ToString().Trim()
    '    tmpEnity1.ContainerName = DT4.Rows(0)("containername").ToString().Trim()
    '    tmpEnity1.Level = "Lot"
    '    tmpEnity1.ServerHostInfo = serverInfo
    '    attList.Add(tmpEnity1)
    'Next

    '更新数据
    'Dim result1 As uMESProcessResult = CommonObject.SaveDenamicAttribute(attList)
    'result.Result = result1.Result
    'If result1.Result = False Then
    '    result.Message = result1.Message
    'End If
#End Region

End Class
